<template>
  <div>
    <el-row>
      <el-button type="primary" size="mini" @click="handleDrillTaskAdd">添加临时演练任务</el-button>
    </el-row>
    <el-table :data="localTasks" size="mini" border :style="{ width: '100%', marginTop: '15px' }" row-key="taskId">
      <el-table-column prop="userName" label="执行人" width="120">
        <template slot-scope="scope">
          <div v-if="type == 2">
            {{ scope.row.userName }}
            <el-button type="text" @click="setTaskExecuteUser(scope.row)"> {{ scope.row.userName != null ? "修改" : "设置" }}</el-button>
          </div>
          <div v-else>
            {{ scope.row.userName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="deptName" label="所属部门" width="120"> </el-table-column>
      <el-table-column prop="groupName" label="所属分组" width="120"> </el-table-column>
      <el-table-column prop="taskContent" label="任务内容"> </el-table-column>
      <el-table-column prop="helpContent" label="辅助信息"> </el-table-column>
      <el-table-column prop="taskContent" label="任务状态" align="center" width="100">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.status == 0" size="mini">待下发</el-tag>
          <el-tag v-if="scope.row.status == 1" size="mini">未完成</el-tag>
          <el-tag type="success" v-if="scope.row.status == 2" size="mini">已完成</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 3" size="mini">已终止</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="taskContent" label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" size="mini" v-if="scope.row.userName != null" @click="chat(scope.row)">任务沟通</el-button>
          <el-button type="text" size="mini" v-if="scope.row.userId != null" @click="handleTaskNotice(scope.row)">任务提醒</el-button>
        </template>
      </el-table-column>
    </el-table>
    <staff-choose-dialog ref="staffChoose" :muilt="false" @Unitpersonnel="onStaffChoose" />
    <drill-task-edit ref="drillTaskEdit" @handleOk="onDrillTaskSave" />
    <ChatIM ref="chatIM" />
    <TaskWarn ref="taskWarnRef" />
  </div>
</template>

<script>
import * as HandleApi from "@/api/response/handle.js";
import * as Auth from "@/utils/auth";
import TaskSubmitView from "@/views/components/taskSubmitView/index.vue";
import StaffChooseDialog from "@/views/components/staffChooseDialog/index.vue";
import DrillTaskEdit from "../components/DrillTaskEdit.vue";
import { getDrillTasksList, drillTaskSet } from "@/api/drill/task.js";
import chatMixin from "@/mixins/chatMixin";
import ChatIM from "@/components/ChatIM/task.vue";
import Cookies from "js-cookie";
import TaskWarn from "../components/TaskWarn.vue";

export default {
  name: "HandleTaskTableView",
  mixins: [chatMixin],
  components: { TaskSubmitView, StaffChooseDialog, DrillTaskEdit, ChatIM, TaskWarn },
  props: {
    tasks: {
      type: Array,
      default: [],
    },
    type: {
      type: Number,
      default: 2,
    },
    handleId: {
      type: Number,
      default: 0,
    },
    levelId: {
      type: Number,
      default: 0,
    },
    time: { type: Number, default: 1000 * 60 * 0.1 },
    planId: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      timer: null,
      localTasks: [],
      taskId: 0,
    };
  },
  mounted() {
    if (this.tasks.length > 0) {
      this.localTasks = this.tasks;
    }
    // 定时查询当前显示的任务列表并更新
    // TODO 设计一个定时任务的总控中心，控制全局所有的子定时任务
    if (this.timer == null) {
      this.timer = setInterval(() => {
        this.getTasks();
      }, this.time);
    }
  },
  methods: {
    getTasks() {
      getDrillTasksList(this.levelId).then(res => {
        this.localTasks = res.data;
        this.$emit("refreshCurrentTasks", res.data);
      });
    },
    setTaskExecuteUser(data) {
      this.taskId = data.taskId;
      this.$refs.staffChoose.showDialog("选择任务执行人");
    },
    // TODO 演练计划的任务是不是要换一个方法 2024年10月30日13:21:32
    onStaffChoose(deptId, userId, user, dept) {
      console.log(deptId, userId, user, dept);
      // 调用设置执行人接口，只有应急处置才需要修改这块的设置，所以直接默认设置就行了
      HandleApi.updateReceiveUser({
        taskId: this.taskId,
        receiveUser: userId,
      }).then(res => {
        this.getTasks();
      });
    },
    handleStopTask(data) {
      // 删除或者取消任务
      this.$modal.loading("正在取消任务，请稍后...");
      HandleApi.stopTask(data.taskId).then(res => {
        this.$modal.closeLoading();
        if (res.code == 200) {
          this.$modal.msgSuccess(res.msg);
        } else {
          this.$modal.msgError(res.msg);
        }
        this.getTasks();
      });
    },
    handleDrillTaskAdd() {
      this.$refs.drillTaskEdit.add(this.levelId);
    },
    onDrillTaskSave(data) {
      drillTaskSet({
        taskId: data.taskId,
        parentId: data.parentId,
        levelId: data.levelId,
        receiveUser: data.receiveUser,
        taskContent: data.taskContent,
        helpContent: data.helpContent,
      }).then(res => {
        this.$modal.msgSuccess("临时任务添加成功");
        this.getTasks();
      });
    },
    chat(row) {
      console.log(row);
      let uid = Auth.getUserId();
      if (row.receiveUser == uid) {
        // 不允许自己跟自己交流
        this.$modal.msgError("不能和自己沟通");
        return false;
      }
      this.initTaskChat("drill", this.planId, row.taskId, row.userName);
    },
    handleTaskNotice(row) {
      this.$refs.taskWarnRef.showDialog(row);
    },
  },
  watch: {
    tasks(n, o) {
      this.localTasks = n;
    },
  },
};
</script>

<style lang="scss" scoped></style>
